package com.qhg.saas_server.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qhg.saas_server.mapper.ProcureMapper;
import com.qhg.saas_server.mapper.SupplierMapper;
import com.qhg.saas_server.pojo.Comployee;
import com.qhg.saas_server.pojo.List;
import com.qhg.saas_server.pojo.ListProduct;
import com.qhg.saas_server.pojo.dto.ProcureListDto;
import com.qhg.saas_server.pojo.dto.ProcureProductDto;
import com.qhg.saas_server.service.ProcureService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

@Service
@Slf4j
public class ProcureServiceImpl implements ProcureService {
    @Autowired
    private ProcureMapper procureMapper;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    HttpSession httpSession;
    @Override//查找所有采购
    public PageInfo findAll() {
        PageHelper.startPage(1,10);
        java.util.List<List> list = procureMapper.findAll();
        PageInfo<List> pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override//审批
    public Integer examine(Integer listId, Integer statusId,String listApprove) {
        return procureMapper.examine(listId,statusId,listApprove);
    }

    @Override//删除采购
    public Integer deleteProcure(Integer listId) {
        procureMapper.deleteProcureList(listId);
        return procureMapper.deleteProcure(listId);
    }

    @Override//添加采购
    public Integer addProcure(ProcureListDto procureListDto) {
//        Integer comploId = (Integer) httpServletRequest.getSession().getAttribute("comploId");
        int i=0;
        procureMapper.addProcure(procureListDto);
        int listId=procureListDto.getListId();
        System.out.println(listId);
        for( ProcureProductDto procureProductDto : procureListDto.getProcureProductDto()){
            procureProductDto.setLp_listId(listId);
            System.out.println(procureProductDto.getLp_productId());
            procureMapper.addProcureList(procureProductDto);
            i++;
        }
        return i;
    }

    @Override//条件查询
    public PageInfo selectProcureByCondition(String beforeTime, String afterTime, String supName, String statusDescription)     {
        PageHelper.startPage(1,10);
        java.util.List<ProcureListDto> procureListDtos = procureMapper.selectProcureByCondition(beforeTime, afterTime, supName, statusDescription);
        PageInfo<ProcureListDto> pageInfo = new PageInfo(procureListDtos);
        return pageInfo;
    }

    @Override//根据id查找采购详情
    public ProcureListDto selectById(Integer listId) {
        ProcureListDto procureListDto= procureMapper.selectByProcureId(listId);
        procureListDto.setProcureProductDto(procureMapper.selectByProcureProductId(listId));
        for (ProcureProductDto p:procureListDto.getProcureProductDto()
             ) {
            p.setProductTotal(p.getNumber()*p.getProductPrice());
        }
        return procureListDto;
    }

    @Override//修改采购
    public int updateProcure(ProcureListDto procureListDto) {
        procureMapper.updateProcure(procureListDto);
        System.out.println(procureListDto.getListId());
        procureMapper.deleteById(procureListDto.getListId());
        for (ProcureProductDto listProduct : procureListDto.getProcureProductDto()
                ) {
            listProduct.setLp_listId(procureListDto.getListId());
            procureMapper.insertById(listProduct);
        }

       return procureMapper.updateProcure(procureListDto);
    }

    @Override
    public java.util.List<Comployee> listComp() {
        return procureMapper.listComp();
    }
}
